#include<unordered_set>
using std::unordered_set;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution
{
public:
    bool hasCycle(ListNode *head)
    {
        unordered_set<ListNode*> mp;
        while (head) {
            if (mp.find(head) != mp.end()) return true;
            mp.insert(head);
            head = head->next;
        }
        return false;
    }
};